//Village random tester

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "interface.h"
#include "dominion.h"
#include "dominion_helpers.h"

int main (int argc, char **argv) {
  struct gameState preplay;
  struct gameState postplay;
  int numPlayers = 0;
  int kingdomCards[] = {adventurer, embargo, mine, cutpurse, smithy, village, gardens, minion, tribute, sea_hag};
  int numTests = 20000;
  int numPassed = 0;
  int numFailed = 0;
  int seed = 200;
  int player = 0;
  int i, j;
  int pass = 1;
  int pos = 0;
  int verb = 0;
  char option;
  while((option = getopt(argc, argv, "hn:s:v")) != -1) {
	switch (option) {
	case 'h':
	  printf("-n [integer] will determine the number of tests to be run. 500 tests is default.\n");
	  printf("-s [integer] will determine the seed to be used. Seed 100 is default.\n");
	  printf("-v will determine if you want the program to be verbose about passing or failing tests.\n");
	  exit(EXIT_SUCCESS);
	case 'n':
	  numTests = atoi(optarg);
	  break;
	case 's':
	  seed = atoi(optarg);
	  break;
	case 'v':
	  verb = 1;
	  break;
	default:
	  printf("Option %c is not a realistic option.\n", option);
	  exit(EXIT_SUCCESS);
	}
  } 
  srand(seed);
  for(i = 0; i < numTests; i++) {
	pass = 1;
	bzero(&preplay, sizeof(struct gameState));
	bzero(&postplay, sizeof(struct gameState));

	while(numPlayers < 2) {
	  numPlayers = rand() % MAX_PLAYERS;
	}

	if(initializeGame(numPlayers, kingdomCards, seed, &preplay) == -1) {
	  printf("ERROR: initializeGame failed.\n");
	  exit(EXIT_FAILURE);
	}
	 
	player = rand() % numPlayers;
	preplay.whoseTurn = player;
	preplay.numActions = (rand() % 5) + 1;
	preplay.handCount[player] = rand() % MAX_HAND;
	preplay.deckCount[player] = rand() % MAX_DECK;
	preplay.discardCount[player] = rand() % MAX_DECK;
	preplay.playedCardCount = rand() % MAX_DECK;
	
	for(j = 0; j < preplay.handCount[player]; j++) {
	  preplay.hand[player][j] = rand() % treasure_map;
	}
   
	for(j = 0; j < preplay.deckCount[player]; j++) {
	  preplay.deck[player][j] = rand() % treasure_map;
	}

	for(j = 0; j < preplay.discardCount[player]; j++) {
	  preplay.discard[player][i] = rand() % treasure_map;
	}
   
	for(j = 0; j < preplay.playedCardCount; j++) {
	  preplay.playedCards[i] = rand() % treasure_map;
	}

	if(preplay.handCount[player] < 2) {
	  pos = 0;
	} else {
	  pos = rand() % (preplay.handCount[player] - 1);
	}

	preplay.hand[player][pos] = village;
	
	memcpy(&postplay, &preplay, sizeof(struct gameState));

	j = playCard(pos, -1, -1, -1, &postplay);

	if(j == -1) {
	  if(verb) {
		printf("playcard function has rejected the arguments.\n");
	  }
	  pass = 0;
	}

	if(preplay.numActions + 1 != postplay.numActions) {
	  if(verb) {
		printf("numactions: %d + 1 -> %d\tseed: %d\ttest: %d\n", preplay.numActions, postplay.numActions, seed, i);
	  }
	  pass = 0;
	}

	if(preplay.playedCardCount+1 != postplay.playedCardCount) {
	  if(verb) {
		printf("playedcardcount: %d + 1 -> %d\tseed: %d\ttest: %d\n", preplay.playedCardCount, postplay.playedCardCount, seed, i);
	  }
	  pass = 0;
	}

	if(preplay.handCount[player] != postplay.handCount[player]) {
	  if(verb) {
		printf("handcount: %d -> %d\tseed: %d\ttest: %d\n", preplay.handCount[player], postplay.handCount[player], seed, i);
	  }
	  pass = 0;
	}

	if((preplay.deckCount[player] - 1 != postplay.deckCount[player]) && (preplay.deckCount[player] != 0)) {
	  if(verb) {
		printf("deckcount: %d - 1 -> %d\tseed: %d\ttest = %d\n", preplay.deckCount[player], postplay.deckCount[player], seed, i);
	  }
	  pass = 0;
	}

	if(pass == 1) {
	  numPassed++;
	} else {
	  numFailed++;
	}
  } 

  if(numPassed == numTests) {
	printf("All %d tests PASSED\t seed = %d\n", numTests, seed);
  } else {
	printf("%d FAILED\t%d PASSED\tseed: %d\n", numFailed, numPassed, seed);
  }

  return 0;
}